package com.zb.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zb.dto.BrandDTO;
import com.zb.dto.CategoryDTO;
import com.zb.dto.SkuDTO;
import com.zb.dto.SpuDTO;
import com.zb.entity.TbBrandModel;
import com.zb.entity.TbCategoryModel;
import com.zb.entity.TbSkuModel;
import com.zb.entity.TbSpuModel;
import com.zb.service.TbBrandService;
import com.zb.service.TbCategoryService;
import com.zb.service.TbSkuService;

import com.zb.service.TbSpuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName TbSkuController
 * @Description 商品表控制器
 * @Author xm
 * @Date 2024/11/06 17:13
 **/
@RestController
@RequestMapping("/tb-sku-model")

	public class TbSkuController {

	@Autowired
	private TbSpuService tbSpuService;
	@Autowired
	private TbCategoryService tbCategoryService;
	@Autowired
	private TbBrandService tbBrandService;

	@Autowired
	public TbSkuService tbSkuModelService;
	@Autowired
	private ConfigurableApplicationContext applicationContext;
	@GetMapping("/page")
	public List<SkuDTO> page(@RequestParam("index")Integer index,
							 @RequestParam("size")Integer size){
		Page<TbSkuModel> page1 = new Page<>();
		page1.setCurrent(index.longValue());
		page1.setSize(size.longValue());
		tbSkuModelService.page(page1);
		List<TbSkuModel> records = page1.getRecords();
		return records.stream().map(item->{
			SkuDTO skuDTO = new SkuDTO();
			BeanUtils.copyProperties(item,skuDTO);
			return skuDTO;
		}).collect(Collectors.toList());
	}

	@GetMapping("/item")
	public Map<String, Object> item(@RequestParam("skuId") String skuId) {
		Map<String, Object> map = new HashMap<>();
		TbSkuModel skuModel = tbSkuModelService.getById(skuId);
		TbSpuModel spuModel = tbSpuService.getById(skuModel.getSpuId());
		TbBrandModel brandModel = tbBrandService.getById(spuModel.getBrandId());
		TbCategoryModel category1Model = tbCategoryService.getById(spuModel.getCategory1Id());
		TbCategoryModel category2Model = tbCategoryService.getById(spuModel.getCategory2Id());
		TbCategoryModel category3Model = tbCategoryService.getById(spuModel.getCategory3Id());

		SkuDTO skuDTO = new SkuDTO();
		BeanUtils.copyProperties(skuModel, skuDTO);
		SpuDTO spuDTO = new SpuDTO();
		BeanUtils.copyProperties(spuModel, spuDTO);
		BrandDTO brandDTO = new BrandDTO();
		BeanUtils.copyProperties(brandModel, brandDTO);
		CategoryDTO c1 = new CategoryDTO();
		BeanUtils.copyProperties(category1Model, c1);
		CategoryDTO c2 = new CategoryDTO();
		BeanUtils.copyProperties(category2Model, c2);
		CategoryDTO c3 = new CategoryDTO();
		BeanUtils.copyProperties(category3Model, c3);
		map.put("skuDTO", skuDTO);
		map.put("spuDTO", spuDTO);
		map.put("brandDTO", brandDTO);
		map.put("c1", c1);
		map.put("c2", c2);
		map.put("c3", c3);
		return map;
	}


	@GetMapping("/info")
	public SkuDTO info(@RequestParam("id")String id){
		TbSkuModel tbSkuModel = tbSkuModelService.getById(id);
		SkuDTO skuDTO = new SkuDTO();
		BeanUtils.copyProperties(tbSkuModel,skuDTO);
		return skuDTO;
	}


	@PostMapping("/update-num")
	public Boolean update(@RequestParam("id")String id,@RequestParam("num")Integer num){
		TbSkuModel tbSkuModel = tbSkuModelService.getById(id);
		tbSkuModel.setNum(tbSkuModel.getNum()-num);
		return tbSkuModelService.updateById(tbSkuModel);
	}

}

